package com.fengmi.sso.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fengmi.comm.ResultVO;
import com.fengmi.sso.SysUser;
import com.fengmi.sso.bcrypt.BCryptPasswordEncoder;
import com.fengmi.sso.mapper.SysUserMapper;
import com.fengmi.sso.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengmi.utils.bean.JwtUtils;
import com.fengmi.utils.bean.RsaUtils;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author sujie
 * @since 2022-11-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public ResultVO login(SysUser user) {
        //1 校验用户名和密码是否正确
           //1.1校验用户名
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("account",user.getAccount());
        sysUserQueryWrapper.eq("user_type","6");
        SysUser sysUser = this.baseMapper.selectOne(sysUserQueryWrapper);
        if(sysUser==null){   //用户名错误
            return  new ResultVO("1001","用户名或密码错误",null);
        }
          //1.2校验密码   ,先把名文加密成密文，密文之间相互比较
        BCryptPasswordEncoder bcript = new BCryptPasswordEncoder();
       if(!bcript.matches(user.getPassword(),sysUser.getPassword())) {
           return  new ResultVO("1001","用户名或密码错误",null);
       }
        //2 正确的话颁发jwt令牌
             //2.1获取私钥
        try {
            //获取私钥路径
            String path =  ResourceUtils.getFile("classpath:rsa_pri").getPath();
            //获取私钥
            PrivateKey privateKey = RsaUtils.getPrivateKey(path);
            //2.2生成token
            sysUser.setPassword("");   //密码脱敏
            String token = JwtUtils.generateTokenExpireInMinutes(sysUser, privateKey, 40);
            return  new ResultVO("1000","登录成功",token);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultVO("1001","登录失败",null);
    }

    @Override
    public ResultVO checkLogin( String token) {
        // 判断头部是否有token，以及token是否合法
        if (token == null) {
            return new ResultVO("1001", "校验失败", null);
        }
        try {
            //获取公钥
            String path = ResourceUtils.getFile("classpath:rsa_pub").getPath();
            PublicKey publicKey = RsaUtils.getPublicKey(path);
            //校验
            JwtUtils.getInfoFromToken(token, publicKey, SysUser.class);
            return new ResultVO("1000", "校验成功", null);
        } catch (ExpiredJwtException e) {
            e.printStackTrace();
            return new ResultVO("1001", "令牌超时", null);
        } catch (MalformedJwtException e) {
            e.printStackTrace();
            return new ResultVO("1001", "非法令牌", null);
        } catch (Exception e) {
            return new ResultVO("1001", "校验失败", null);
        }
    }
}
